package com.sample.action;


import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sample.action.form.MenuForm;
import com.sample.javabean.Area;
import com.sample.javabean.Menu;
import com.sample.util.Pager;


/**
 * 菜单管理模块
 * 
 * @author 演示
 * 
 */
public class MenuAction extends BaseAction {

	private final static Log log = LogFactory.getLog(MenuAction.class);
	private MenuForm menuForm = new MenuForm();
	private Integer id;
	private Pager<Menu> pageList;
	private List<Menu> rootMenuList;
	private String keywords;
	
	// 使用new()操作符实例化领域模型
	private Menu menu = new Menu();

	/**
	 * 添加菜单
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add()throws Exception{
		try{
			log.info("add Menu:"+menuForm.getMenuname());
			if(menuForm!=null){
				menu.setPid(menuForm.getPid());
				menu.setMenuurl(menuForm.getMenuurl());
				menu.setMenuname(menuForm.getMenuname());
				menu.setRemark(menuForm.getRemark());
				if(menuForm.getPid() == null || menuForm.getPid() == 0){
					menu.setType("0");
				}else{
					menu.setType("1");
				}
				
				menu.addMenu();	
			}
			return SUCCESS;
		}catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		
	}

	/**
	 * 显示菜单记录列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String listMenu() throws Exception {
		try {

			pageList = menu.list(page, size, menuForm,keywords);
			return "listMenu";
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}

	}

	public String inputMenu() throws Exception {
		try {
			Menu menu = new Menu();
			// 加载根菜单信息
			rootMenuList = menu.findAll();

			return "inputMenu";
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public String editMenu() throws Exception {
		try {
			menu.setId(id);
			menu.load();
			Menu menu = new Menu();
			// 加载根菜单信息
			rootMenuList = menu.findAll();
			return "editMenu";
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		try {
			if (menuForm != null) {
				menu.setId(menuForm.getId());
				menu.setPid(menuForm.getPid());
				menu.setMenuurl(menuForm.getMenuurl());
				menu.setMenuname(menuForm.getMenuname());
				menu.setRemark(menuForm.getRemark());
				if(menuForm.getPid() == 0){
					menu.setType("0");
				}else{
					menu.setType("1");
				}
				menu.editMenu();
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String del() throws Exception {
		try {
			if (menuForm != null) {
				menu.setId(id);
				menu.delMenu();
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}



	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public MenuForm getMenuForm() {
		return menuForm;
	}

	public void setMenuForm(MenuForm menuForm) {
		this.menuForm = menuForm;
	}

	public Pager<Menu> getPageList() {
		return pageList;
	}

	public void setPageList(Pager<Menu> pageList) {
		this.pageList = pageList;
	}

	public Menu getMenu() {
		return menu;
	}

	public List<Menu> getRootMenuList() {
		return rootMenuList;
	}

	public void setRootMenuList(List<Menu> rootMenuList) {
		this.rootMenuList = rootMenuList;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}
}
